/**
 * 日期处理
 */
import { ref } from 'vue'
import { WeekCalendarData } from '../../../types/tn-week-calendar'

 type CalendarData = {
  value: string | number,
  desc: string,
  select: boolean,
  disabled: boolean
}

export default function useDateInfo() {

  // 星期对应的中文
  const weekdayZh: string[] = ['日','一','二','三','四','五','六']

  // 配置参数
  let selectValue = ref<string | number>('')
  let year = ref<number>(0)
  let month = ref<number>(0)
  let dateData = ref<WeekCalendarData[]>([])

  let currentYear: number = 0
  let currentMonth: number = 0

  // 日期数据
  let calendarData = ref<CalendarData[][]>([])
  let currentShowWeek = ref<number>(0)

  // 初始化数据
  const init = (
    _value: string | number,
    _year: string | number,
    _month: string | number,
    _data: WeekCalendarData[]
  ) => {
    const date = new Date()
    currentYear = date.getFullYear()
    currentMonth = date.getMonth() + 1
    year.value = _year ? Number(_year) : currentYear
    month.value = _month ? Number(_month) : currentMonth
    dateData.value = _data

    if (!_value) {
      _value = date.getDate()
    }

    setDefaultValue(_value)
  }

  // 设置默认值值
  const setDefaultValue = (val: string | number) => {
    selectValue.value = val

    generateCalendarArr()
  }

  // 更新切换周后的数据
  const updateCurrentShowWeek = (val: number) => {
    currentShowWeek.value = val
  }

  // 更新选中的日期
  const updateSelectValue = (val: string | number) => {
    for (let i = 0; i < calendarData.value.length; i++) {
      for (let j = 0; j < calendarData.value[i].length; j++) {
        if (calendarData.value[i][j].value == val) {
          calendarData.value[i][j].select = true
        } else {
          calendarData.value[i][j].select = false
        }
      }
    }
  }

  // 生成日期数据
  const generateCalendarArr = () => {
    // 空白天数
    const emptyDayNumber = _getMonthFirstWeekDay(year.value, month.value)
    // 当前月份的天数
    const monthDayNumber = _getMonthDay(year.value, month.value)

    // 建立数据
    let data: CalendarData[] = []
    // 填充空白天数
    // console.log(emptyDayNumber);
    if (emptyDayNumber > 0) {
      data = Array(emptyDayNumber).fill({
        value: '',
        desc: '',
        select: false,
        disabled: true
      })
    }

    // 填充日期数据
    for (let i = 1; i <= monthDayNumber; i++) {

      let select: boolean = false
      let desc: string = ''
      if (selectValue.value === i) {
        select = true
      }

      // 判断当前日期是否有设置对应的值
      const _dateData = dateData.value.find((item) => item.day == i)
      if (_dateData) {
        desc = _dateData?.content || ''
      }

      data.push({
        value: i,
        desc: desc,
        select: select,
        disabled: false
      })
    }

    // 分割数组, 将数组分割成7天为一组的数据
    for (let i = 0; i < Math.ceil(data.length / 7); i++) {
      const weekDate = data.slice( 7 * i, 7 * i + 7)
      if (month.value === currentMonth && weekDate.findIndex((item) => {
        return item.select
      }) !== -1) {
        currentShowWeek.value = i
      }
      calendarData.value.push(weekDate)
    }
  }

  // 获取对应月份有多少天
  const _getMonthDay = (_year: number, _month: number): number => {
    return new Date(_year, _month, 0).getDate()
  }
  // 获取对应月份的第一天是星期几
  const _getMonthFirstWeekDay = (_year: number, _month: number): number => {
    return new Date(`${_year}/${_month}/01 00:00:00`).getDay()
  }

  return {
    weekdayZh,
    calendarData,
    currentShowWeek,
    selectValue,
    init,
    setDefaultValue,
    updateCurrentShowWeek,
    updateSelectValue
  }
}